package DAO;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import Classes.Cliente;
import Classes.Exemplar;
import Classes.Locacao;
import Nucleo.ConexaoBanco;

public class LocacaoDAO {

	private ConexaoBanco conn;
	
	public LocacaoDAO(){
		conn = new ConexaoBanco();
	}
	
	public void inserirLocacao( Cliente cliente , Exemplar exemplar , String dataDevolucao){
		
		String sql = "INSERT INTO `locacao` (`idExemplar`,`idCliente`,`dt_devolucao`," +
				"values ( ? , ? , ?) ";
		try {
			SimpleDateFormat formatarDate = new SimpleDateFormat("dd/MM/yyyy");
			Date d = new Date();
			try {
				d = formatarDate.parse(dataDevolucao);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			PreparedStatement stmt = conn.getConnection().prepareStatement(sql);
			stmt.setInt	  	(1, exemplar.getId() );
			stmt.setInt		(2, cliente.getId() );
			stmt.setDate	(3, new java.sql.Date( d.getTime() ) );	
			stmt.execute();
			stmt.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
	}
	
	public List<Locacao> getLocacoes(){
		String sql = "SELECT * FROM `locacao`";
		List<Locacao> lista = new ArrayList<Locacao>();
		
		try {
			PreparedStatement stmt = conn.getConnection().prepareStatement(sql);
			ResultSet rs = stmt.executeQuery();
						
			while(rs.next()) {
				SimpleDateFormat formatarDate = new SimpleDateFormat("yyyy-MM-dd");
				
				Locacao locacao = new Locacao();	
				
				ExemplarDAO exemplarDao = new ExemplarDAO();
				ClienteDAO clienteDao = new ClienteDAO();
				
				locacao.setCliente( clienteDao.getClientePeloId( rs.getInt("idCliente") ) );
				locacao.setExemplar( exemplarDao.getExemplarPeloId( rs.getInt("idExemplar") ) );
				locacao.setDataDevolucao( formatarDate.format( rs.getDate("dt_devolucao") ) );
				
				lista.add( locacao );
			}
			
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return lista;
	}
	
	public List<Locacao> getLocacoesCliente( int idCliente ){
		String sql = "SELECT * FROM `locacao` WHERE `idCliente` = '"+idCliente+"'";
		List<Locacao> lista = new ArrayList<Locacao>();
		
		try {
			PreparedStatement stmt = conn.getConnection().prepareStatement(sql);
			ResultSet rs = stmt.executeQuery();
						
			while(rs.next()) {
				SimpleDateFormat formatarDate = new SimpleDateFormat("yyyy-MM-dd");
				
				Locacao locacao = new Locacao();	
				
				ExemplarDAO exemplarDao = new ExemplarDAO();
				ClienteDAO clienteDao = new ClienteDAO();
				
				locacao.setCliente( clienteDao.getClientePeloId(idCliente) );
				locacao.setExemplar( exemplarDao.getExemplarPeloId( rs.getInt("idExemplar") ) );
				locacao.setDataDevolucao( formatarDate.format( rs.getDate("dt_devolucao") ) );
				
				lista.add( locacao );
			}
			
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return lista;
	}

	public boolean getExemplarIsLocado( int idExemplar ){
		
		for (Locacao loc : this.getLocacoes()) {
			if ( loc.getExemplar().getId() == idExemplar ){
				return true;
			}
		}
		
		return false;
	}

	public boolean deletarPeloExemplar(int idExemplar) {

		String sql = "DELETE FROM `Locacao` WHERE `idExemplar` = '"+ idExemplar+"'";
		
		try {
			Statement stmt = conn.getConnection().createStatement();
			int delete = stmt.executeUpdate(sql);
			if ( delete == 1) {
				return true;
			} else {
				return false;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
		
		
	}
}
